package com.yidu.hbats.action.TrainingPlan;

import com.yidu.hbats.action.vo.AtsPlanWeekvo;
import com.yidu.hbats.model.*;
import com.yidu.hbats.service.TrainingPlan.AtsEmpAthleteSchemeService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanItemService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanPhaseService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanWeekService;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.utils.AtsPlanItemTree;
import com.yidu.utils.FileTools;
import com.yidu.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩红
 * @类的描述——
 * @date 2019/10/30下午 04:01
 */
@Controller
@RequestMapping("atsPlanWeekController")
public class AtsPlanWeekController {
    @Resource
    AtsPlanWeekService atsPlanWeekService;
    @Resource
    AtsPlanPhaseService planPhaseService;

    //体育训练表的注入
    @Resource
    AtsBaseSportService atsBaseSportService;
    //教练表的注入
    @Resource
    AtsEmpCoachService atsEmpCoachService;
    //指标表
    @Resource
    AtsPlanItemService atsPlanItemService;
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;
    //文件上传管理工具
    @Resource
    FileTools fileTools;

    /**
     * 根据Id查询周计划
     * @param atsPlanItem
     * @param astPlanWeek
     * @return
     */
    @RequestMapping("weekById")
    @ResponseBody
    public Map<String, Object> findById(AtsPlanItem atsPlanItem, AtsPlanWeek astPlanWeek) {
        Map<String, Object> map = atsPlanItemService.weekSelectPid(atsPlanItem, astPlanWeek);
        return map;
    }

    /**
     * 周计划的图表
     * @param atsPlanItem
     * @return
     */
    @RequestMapping("weekEchar")
    @ResponseBody
    public List<AtsPlanItemTree> echar(AtsPlanItem atsPlanItem) {
        List<AtsPlanItemTree>  treeList = atsPlanItemService.weekEchar(atsPlanItem);
        return treeList;

    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @RequestMapping("delAll")
    @ResponseBody
    public Message delAll(String id) {
        int rows = atsPlanWeekService.delAll(id);
        Message mes = new Message();
        if (rows >= 1) {
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }


        return mes;
    }

    /**
     * 逻辑删除
     * @param astPlanWeek
     * @return
     */
    @RequestMapping("updateIsva")
    @ResponseBody
    public Message updateIsva(AtsPlanWeek astPlanWeek) {
        //调用逻辑删除
        int rows = atsPlanWeekService.updateIsva(astPlanWeek);
        Message mes = new Message();
        //删除计划同时删除下面指标
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setPid(astPlanWeek.getWeekId());
        //删除对应的运动员
        AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
        atsEmpAthleteScheme.setIsva(0);
        atsEmpAthleteScheme.setPid(astPlanWeek.getWeekId());
        if (rows >= 1) {
            int rowso=atsEmpAthleteSchemeService.updatePidFour(atsEmpAthleteScheme);
            int rowst=atsPlanItemService.updateOrdeletethree(atsPlanItem);
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }


        return mes;
    }

    /**
     * 查询所有
     * @param request
     * @param atsPlanWeek
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String, Object> selectAll(HttpServletRequest request,AtsPlanWeek atsPlanWeek, @RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize) {
        int count = atsPlanWeekService.count(request,atsPlanWeek);
        List<AtsPlanWeek> list = atsPlanWeekService.selectAll(request,atsPlanWeek,pageNum,pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);

        map.put("count", count);

        map.put("code", 0);

        map.put("msg", "");//消息
        return map;
    }

    /**
     * 增加或者修改
     * @param atsPlanWeekvo
     * @return
     */
    @RequestMapping("addOrupdate")
    @ResponseBody
    public Message addOrupdate(AtsPlanWeekvo atsPlanWeekvo){
        atsPlanWeekvo.setRemark("2");
        atsPlanWeekvo.setIsva(1);
        //根据下拉框的教练id查出教练名
        AtsEmpCoach atsEmpCoach=atsEmpCoachService.findById(atsPlanWeekvo.getCoachId());
        //根据下拉框的阶段id查出阶段名
        AtsPlanPhase atsPlanWeek=planPhaseService.selectByPrimaryKey(atsPlanWeekvo.getPhaseId());
        //传入体育项目名
        atsPlanWeekvo.setItemName(atsPlanWeekvo.getSportName());
        //传入教练名
        atsPlanWeekvo.setCoachName(atsEmpCoach.getCoachName());
        //传入阶段名
        atsPlanWeekvo.setPhaseName(atsPlanWeek.getPhaseName());
        //新建信息类
        Message message=new Message();
        //判断附件是否上传成功
        if(atsPlanWeekvo.getFileone().getOriginalFilename()!=null &&!"".equals(atsPlanWeekvo.getFileone().getOriginalFilename())){
            try {
                String file1=fileTools.saveFile("TrainingPlan",atsPlanWeekvo.getFileone());
                atsPlanWeekvo.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(atsPlanWeekvo.getFiletwo().getOriginalFilename()!=null &&!"".equals(atsPlanWeekvo.getFiletwo().getOriginalFilename())){
            try {
                String file2 = fileTools.saveFile("TrainingPlan",atsPlanWeekvo.getFiletwo());
                atsPlanWeekvo.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(atsPlanWeekvo.getFilethree().getOriginalFilename()!=null &&!"".equals(atsPlanWeekvo.getFilethree().getOriginalFilename())){
            try {
                String file3=fileTools.saveFile("TrainingPlan",atsPlanWeekvo.getFilethree());
                atsPlanWeekvo.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //ManagerModel managerModel= (ManagerModel) session.getAttribute("manager");
        //取到当前登录人
        //managerModel.getManUser();
        //atsPlanWeekvo.setCreater("admin");
        //给指标传创建人
        //atsPlanYearMorevo.setCreateName("");
        if(atsPlanWeekvo.getWeekId()!=null &&!"".equals(atsPlanWeekvo.getWeekId())){
            atsPlanWeekService.update(atsPlanWeekvo);
        }else{
            atsPlanWeekService.insertSelective(atsPlanWeekvo);
        }
        //传成功
        message.setNum(1);
        message.setMsg("操作成功!");
        return message;
    }

    /**
     * 根据Id查询
     * @param weekId
     * @return
     */
    @RequestMapping("findById")
    @ResponseBody
    public AtsPlanWeek findById(Integer weekId){
        AtsPlanWeek atsPlanWeek=atsPlanWeekService.selectByPrimaryKey(weekId);
        return atsPlanWeek;
    }

    /**
     * 查询所有
     * @param atsPlanWeek
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public List<AtsPlanWeek> selectAll(AtsPlanWeek atsPlanWeek){
        List<AtsPlanWeek> list=atsPlanWeekService.selectAll(atsPlanWeek);
        return list;
    }

    /**
     * 根据Id查询
     * @param creater
     * @return
     */
    @RequestMapping("findIdTwo")
    @ResponseBody
    public List<AtsPlanWeek> findIdTwo(String creater){
        List<AtsPlanWeek> list=atsPlanWeekService.findIdTwo(creater);
        return list;
    }
    /**
     * 查询当前最大的id
     * @return
     */
    @ResponseBody
    @RequestMapping("findMaxPid")
    public AtsPlanWeek findMaxPid(int coachId){
        AtsPlanWeek atsPlanWeek=atsPlanWeekService.findMaxPid(coachId);
        return atsPlanWeek;
    }

    /**
     * 查询上级计划
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("findPlanItemYearMore")
    public List<AtsPlanItem> findPlanItemYearMore(Integer pid){
        List<AtsPlanItem> list=atsPlanItemService.findPlanItemYearMorethree(pid);
        return list;
    }

    /**
     * 查询上级计划
     * @param pid
     * @return
     */
    @ResponseBody
    @RequestMapping("findPlanItemYearMoreone")
    public List<AtsPlanItem> findPlanItemYearMoreone(Integer pid){
        List<AtsPlanItem> list=atsPlanItemService.findPlanItemYearMorethree(pid);
        return list;
    }

    /**
     * 删除指标
     * @param itemId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteZhibiao")
    public Message deleteZhibiao(Integer itemId){
        //新建计划类
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setItemId(itemId);
        Message message=new Message();
        //调用逻辑删除方法
        int rows=atsPlanItemService.updateByPrimaryKeySelectivethree(atsPlanItem);
        if(rows>0){
            message.setMsg("删除成功!");
            message.setNum(1);
        }else{
            message.setMsg("删除失败!");
            message.setNum(0);
        }
        return message;
    }

    /**
     * 查询数据
     * @return
     */
    @ResponseBody
    @RequestMapping("findDivone")
    public List<AtsPlanItem> findDivone(){
        List<AtsPlanItem> list=atsPlanItemService.findDivthree();
        return list;
    }
}
